package cn.tmpan.project.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

/**
 * The type Swagger config.
 *
 * @author tieMinPan
 * @since  2019 -05-24 15:06
 */
@Configuration
public class SwaggerConfig {

    /**
     * The Swagger enabled.
     *
     * 是否开启swagger ，正式环境一般是需要关闭的，可根据springboot的多环境配置进行设置
     */
    @Value(value = "${swagger.enabled}")
    Boolean swaggerEnabled;

    /**
     * Create rest api docket.
     *
     * @return the docket
     */
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
                /**
                 *是否开启
                 */
                .enable(swaggerEnabled).select()
                /**
                 * 扫描的路径包
                 */
                .apis(RequestHandlerSelectors.basePackage("cn.tmpan.project.controller"))
                /**
                 * 指定路径处理PathSelectors.any()代表所有的路径
                 */
                .paths(PathSelectors.any()).build().pathMapping("/");
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("项目接口文档")
                /**
                 * 描述
                 */
                .description("tieMinPan")
                /**
                 * 作者信息
                 */
                .contact(new Contact("tieMinPan", "https://gitee.com/tmpan/", "1255070026@qq.com"))
                .version("1.0.0")
                .build();
    }
}
